//
//  YSPEmoticonPackageModel.swift
//  EmotionIcon
//
//  Created by 朴裕识 on 15/12/29.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPEmoticonPackageModel: NSObject {
    /// id表情包的文件夹名称
    var id: String
    
    /// 表情包的名称
    var group_name_cn: String?
    
    /// 所有的表情模型
    var emoticonModelArray: [YSPEmoticonModel] = [YSPEmoticonModel]()
    
    static let emoticonPackageModelArray: [YSPEmoticonPackageModel] = YSPEmoticonPackageModel.loadEmoticonModelArray()!
    
    init(id: String) {
        self.id = id
        super.init()
    }
    
    static let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!
    
    private class func loadEmoticonModelArray() -> [YSPEmoticonPackageModel]? {
        //拼接emoticon.plist的文件路径
        let plistPath = (bundlePath as NSString).stringByAppendingPathComponent("emoticons.plist")
        //将emoticon.plist转成字典
        let emoticonDict = NSDictionary(contentsOfFile: plistPath)!
        
        if let packageArray = emoticonDict["packages"] as? [[String: AnyObject]] {
            var desArray = [YSPEmoticonPackageModel]()
            
            //添加一个"最近"表情包，里面是空的
           let recent = YSPEmoticonPackageModel(id: "")
            recent.group_name_cn = "最近"
            recent.appendEmptyEmoticon()
            desArray.append(recent)
            
            //循环添加三个有表情的表情包
            for package in packageArray {
                let id = package["id"] as! String
                let packageModel = YSPEmoticonPackageModel(id: id)
                packageModel.loadEmoticonModel()
                desArray.append(packageModel)
            }
            return desArray
        }
        return nil
    }
    
    
     //上面的那个类方法是返回一个数组，里面是表情包包包模型，同时方法里也调用了这个方法，给自身的模型数组属性也赋值了。这样只要外部调用类方法，即可以得到表情包数组，也可以通过表情包数组里面的表情包得到所有表情
    func loadEmoticonModel() {
        //拼接表情文件夹里面的info.plist路径
        let infoPlistPath = ((YSPEmoticonPackageModel.bundlePath as NSString).stringByAppendingPathComponent(id) as NSString).stringByAppendingPathComponent("info.plist")
        //将info.plist转成字典
        let infoDict = NSDictionary(contentsOfFile: infoPlistPath)!
        group_name_cn = infoDict["group_name_cn"] as? String
        
        if let emoticonArray = infoDict["emoticons"] as? [[String: AnyObject]] {
            var index = 0
            for dict in emoticonArray {
                
                let emoticonModel = YSPEmoticonModel(id: id, dict: dict)
                
                emoticonModelArray.append(emoticonModel)
                
                index++
                if index == 20 {
                    emoticonModelArray.append(YSPEmoticonModel(removeEmoticon: true))
                    index = 0
                }
            }
        }
        //加载完所有表情后看看要不要添加空白表情
        appendEmptyEmoticon()
    }
    
    //将点击过的表情添加入最近表情包
    class func appendRecentEmoticon(emoticonModel: YSPEmoticonModel) {
        if emoticonModel.removeEmoticon {
            return //删除按钮不添加入最近表情包
        }
        if emoticonModel.pngPath == nil && emoticonModel.emoji == nil {
            return //空白的表情也不要加进来
        }
        
        emoticonModel.times++
        
        //修改最近表情包模型
        var recentModelArray = emoticonPackageModelArray[0].emoticonModelArray
        
        let removeEmoticon = recentModelArray.removeLast()
        var temp = false
        
        //判断最近表情包里是否有传进来的模型，防止重复添加
        if !recentModelArray.contains(emoticonModel) {
            recentModelArray.append(emoticonModel)
            temp = true
        }
        //排序
        recentModelArray = recentModelArray.sort { (e1, e2) -> Bool in
            return e1.times > e2.times
        }
        
        if temp {
            recentModelArray.removeLast()
        }
        recentModelArray.append(removeEmoticon)
        //这里需要注意，在OC里面，rMA改变了，ePMA.eMA也会跟着改变
        //但是在Swift里面，是不会跟着改变的，因此要重新赋值
        emoticonPackageModelArray[0].emoticonModelArray = recentModelArray

    }
    
    func appendEmptyEmoticon() {
        //判断表情包最后一页是不是满21个
        let count = emoticonModelArray.count % 21
        
        //count = 0说明刚好21个
        //count != 0 说明这个表情包最后一页有count个
        //count ==0这个条件是用来给最近表情包添加空白表情用的，因为空白表情的count%21也是0
        if count > 0 || count == 0 {
            for _ in count..<20 {
                //将第count个至20个全部变成空白表情
                emoticonModelArray.append(YSPEmoticonModel(removeEmoticon: false))
            }
            //第21个变成删除按钮
            emoticonModelArray.append(YSPEmoticonModel(removeEmoticon: true))
        }
        
    }

    // 对象的打印
    override var description: String {
        let p = ["id", "group_name_cn", "emoticons"]
        return "\n\t表情包模型: \(dictionaryWithValuesForKeys(p))"
    }
}
